package com.system.manager.modules.projectLibrary.controller;


import com.system.manager.common.annotation.Anonymous;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.authorized.mapper.UserMapper;
import com.system.manager.modules.projectLibrary.dto.UsernamePassword;
import com.system.manager.modules.projectLibrary.entity.Enterprise;
import com.system.manager.modules.projectLibrary.service.IEnterpriseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2021-11-03
 */
@Api(tags = "单位API")
@RestController
@RequestMapping("/projectLibrary/enterprise")
@Slf4j
public class EnterpriseController {

    @Autowired
    private IEnterpriseService iEnterpriseService;

    @ApiOperation("项目负责人录入单位信息")
    @Anonymous
    @PostMapping("/insert/enterprise")
    public Result insertEnterprise(@RequestBody Enterprise enterprise) {
        try {
            //插入单位信息
            UsernamePassword usernamePassword = iEnterpriseService.insertEnterprise(enterprise);
            return Result.ok(usernamePassword);
        } catch (SysException e) {
            e.printStackTrace();
            //插入单位信息异常
            return Result.error(e);
        }
    }

    @ApiOperation("修改单位信息")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @PostMapping("/update/enterprise")
    public Result updateEnterprise(@RequestBody Enterprise enterprise, @CurrSubject Subject subject) {
        try {
            //修改单位信息
            iEnterpriseService.updateEnterprise(enterprise, subject);
            return Result.ok();
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("展示本单位的信息")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/select/enterprise")
    public Result selectEnterprise(@CurrSubject Subject subject) {
        try {
            Enterprise enterprise = iEnterpriseService.selectEnterprise(subject);
            return Result.ok(enterprise);
        }catch (SysException e){
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation("单位审核")
    @PreAuthorize({SubjectType.ENTERPRISE_ADMIN, SubjectType.zx})
    @GetMapping("/audit/enterprise")
    public Result auditEnterprise(@RequestParam Integer id, @CurrSubject Subject subject, Integer verify) {
        try {
            String message = iEnterpriseService.auditEnterprise(id, subject, verify);
            return Result.ok(message);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询所有申请公司")
    @PreAuthorize(SubjectType.zx)
    @GetMapping("/select/all/enterprise")
    public Result selectAllEnterprise(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                      @RequestParam(name = "limit", required = false, defaultValue = "10") Integer size,
                                      @RequestParam(name = "name", required = false) String name,
                                      @RequestParam(name = "code", required = false) String code,
                                      @RequestParam(name = "username", required = false) String username,
                                      @RequestParam(name = "verify", required = false) String verifyStr

    ) {
        try {
            Result result = iEnterpriseService.selectAllEnterprise(page, size, name, code, username, verifyStr);
            return result;
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询所有公司")
    @PreAuthorize(SubjectType.zx)
    @GetMapping("/select/init/enterprise")
    public Result selectInitEnterprise(){
        try {
            List<Enterprise> enterprises = iEnterpriseService.selectInitEnterprise();
            return Result.ok(enterprises);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
}
